/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.metasketch.model.loader.links;

import java.util.Collection;
import java.util.HashSet;

import org.metasketch.model.loader.GraphNode;
import org.metasketch.model.loader.IGetLinkLogic;

/**
 *
 * @author guilhermegrg
 */
public class ActionAndTaksLinkProvider extends IGetLinkLogic {

    @Override
    public boolean canProcess(GraphNode node, String type, String targetType) {
        if (type.equalsIgnoreCase("Action") || type.equalsIgnoreCase("Task")) {
            return true;
        }
        return false;
    }

    @Override
    public Collection<GraphNode> getLinks(GraphNode node, String type) {

        Collection<GraphNode> links = new HashSet();
        Collection<GraphNode> visited = new HashSet();
        visited.add(node);

        Collection<GraphNode> directLinks = gdb.getDirectRelatedNodes(node);
        for (GraphNode link : directLinks) {
            if (visited.contains(link)) {
                continue;
            }
            visited.add(link);

            String ntype =  link.getType();
            if (type == null || ntype.equals(type)) {
                links.add(link);
            }

        }

        Collection<GraphNode> containing = gdb.getContainingNodes(node);
        for (GraphNode link : containing) {
            if (visited.contains(link)) {
                continue;
            }
            visited.add(link);

            String ntype = link.getType();
            if (type == null || ntype.equals(type)) {
                links.add(link);
            }

        }

        return links;

    }
}
